package com.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet(urlPatterns = "/authCode")
public class AuthCode extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建一个BufferedImage,可以在里面绘制文字
        BufferedImage img=new BufferedImage(80, 30, BufferedImage.TYPE_INT_RGB);
        //创建一个Graphics ,可以将它绘制到bufferedImage
        Graphics graphics = img.createGraphics();
        //设置画笔颜色
        graphics.setColor(Color.ORANGE);
        //设置字体,字体名称,字体样式,字体大小
        graphics.setFont(new Font(Font.MONOSPACED,Font.BOLD,16));
        //生成随机数
        String authCode=new Random().nextInt(10000) +"";
        graphics.drawString(authCode, 20, 20);
        ImageIO.write(img, "jpg", response.getOutputStream());
    }
}
